// pages/home/searchresult/searchresult.js
import dayjs from 'dayjs';
import Toast from '@vant/weapp/toast/toast';
import { actiStatEnum } from '../../common/enums';
import { getFileType } from '../../utils/file';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    scrollHeight: '100vh',
    keyWord: '',
    active: '1',
    actiList: [],
    userList: [],
    storeList: [],
  },
  // 搜索确认
  onSearch(event) {
    this.setData({ keyWord: event.detail });
    this.qryActiList();
  },
  // 搜索清空
  onSearchClear() {
    this.setData({ keyWord: "" });
    this.qryActiList();
  },
  // Tab页切换
  onChangeTab(event) {
    this.setData({
      active: event.detail.name
    })
    this.qryActiList();
  },
  // 刷新活动列表数据
  qryActiList() {
    Toast.loading({
      message: '加载中...',
      forbidClick: true,
    });
    const qryParam = {
      actiTitle: this.data.keyWord
    }
    getApp().callContainer('/api/actiQuery/list', qryParam, (res) => {
      if (res.data.data.actiList != null) {
        for (const acti of res.data.data.actiList) {
          acti.actiStat_label = actiStatEnum.get(acti.actiStatus);
          acti.actiStat_color = this.getActiStatColor(acti.actiStatus);
          acti.imgUrlList = acti.actiImgUrlList.split(',');
          acti.actiBeginTime_label = dayjs(acti.actiBeginTime).format('YYYY-MM-DD HH:mm:ss');
        }
      }
      this.setData({ actiList: res.data.data.actiList || [] });
      this.loadTmpFile();
      Toast.clear();
    }, () => {
      Toast.clear();
    })
  },
  // 缓存图片
  loadTmpFile() {
    for (const acti of this.data.actiList) {
      if (acti.imgUrlList) {
        acti.tmpUrlList = [];
        for (const imgUrl of acti.imgUrlList) {
          getApp().getTempFilePath(imgUrl).then((res) => {
            const type = getFileType(imgUrl);
            acti.tmpUrlList.push({ url: res, type: type });
            if (acti.tmpUrlList.length == acti.imgUrlList.length) {
              this.setData({ actiList: this.data.actiList });
            }
          })
        }
      }
    }
  },
  // 活动状态展示不同颜色
  getActiStatColor(actiStat) {
    if (actiStat == 2) {
      return 'bg-gradual-dd-orange';
    } else if (actiStat == 3) {
      return 'bg-gradual-dd-green';
    } else if (actiStat == 4) {
      return 'bg-gradual-dd-purple';
    } else if (actiStat == 5) {
      return 'bg-gradual-dd-red';
    } 
  },
  onClickActiDetl(event) {
    const data = event.currentTarget.dataset.operation;
    wx.navigateTo({
      url: '/pages/user/acti/detail/detail?actiId=' + data.actiId,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.keyWord) {
      this.setData({ keyWord: options.keyWord });
      this.qryActiList();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.qryActiList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.qryActiList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})